package com.mx.zb.mng.utils;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;


@Component
public class RestInter {
	
Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired  
    RestTemplate rest;
	
	
	private static final Map<String,String> cookie = new HashMap<>();
	
	/**
	 * post方法封装，参数不是以JSON方式，而是传统的FORM键值对
	 * @param url
	 * @param param
	 * @param clz
	 * @return
	 */
	public <T> ResponseEntity<T> postParam(String url, Object param, Class<T> clz) {
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);			
		LinkedMultiValueMap<String, String> hashMap = new LinkedMultiValueMap<>();
		if(param!=null) {
			Map map = JSONObject.parseObject(JSONObject.toJSONString(param), Map.class);
			for(Object key: map.keySet()) {
				hashMap.add(key.toString(), map.get(key).toString());
			}
		}
		
		
		HttpEntity<LinkedMultiValueMap<String, String>> requestEntity = new HttpEntity<LinkedMultiValueMap<String, String>>(hashMap, httpHeaders);
		
		ResponseEntity<T> result = null;
		try {
			result = rest.exchange(url, HttpMethod.POST,requestEntity, clz);
		} catch (RestClientException e) {
			// TODO Auto-generated catch block
			log.error("错误信息：",e);
		} 
		
		return result;
	}
	
	public <T> T postParamBody(String url, Object param, Class<T> clz) {
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);			
		LinkedMultiValueMap<String, String> hashMap = new LinkedMultiValueMap<>();
		if(param!=null) {
			Map map = JSONObject.parseObject(JSONObject.toJSONString(param), Map.class);
			for(Object key: map.keySet()) {
				hashMap.add(key.toString(), map.get(key).toString());
			}
		}
		
		
		HttpEntity<LinkedMultiValueMap<String, String>> requestEntity = new HttpEntity<LinkedMultiValueMap<String, String>>(hashMap, httpHeaders);
		
		T result = null;
		try {
			result = rest.exchange(url, HttpMethod.POST,requestEntity, clz).getBody();
		} catch (RestClientException e) {
			// TODO Auto-generated catch block
			log.error("错误信息：",e);
		} 
		
		return result;
	}
	
	/**
	 * post方法封装，参数不是以JSON方式，而是传统的FORM键值对
	 * @param url
	 * @param clz
	 * @param param 必须是KEY和VALUE的一对一对的
	 * @return
	 */
	public <T> ResponseEntity<T> postParam(String url, Class<T> clz, String... param ) {
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);	
		
		LinkedMultiValueMap<String, String> hashMap = new LinkedMultiValueMap<>();
		if(param != null && param.length % 2 == 0) {
			for(int i=0;i<param.length;i+=2) {
				hashMap.add(param[i], param[i+1]);
			}
		}		
		
		HttpEntity<LinkedMultiValueMap<String, String>> requestEntity = new HttpEntity<LinkedMultiValueMap<String, String>>(hashMap, httpHeaders);
			
		ResponseEntity<T> result = null;
		try {
			result = rest.exchange(url, HttpMethod.POST,requestEntity, clz);
		} catch (RestClientException e) {
			// TODO Auto-generated catch block
			log.error("错误信息：",e);
		} 
		
		return result;
	}
	
	/**
	 * post方法封装，参数以JSON方式
	 * @param url
	 * @param param
	 * @param clz
	 * @return
	 */
	public <T> ResponseEntity<T> postJson(String url, Object param, Class<T> clz) {
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
		HttpEntity<String> requestEntity = new HttpEntity<String>(JSONObject.toJSONString(param), httpHeaders);
			
		ResponseEntity<T> result = null;
		try {
			result = rest.exchange(url, HttpMethod.POST,requestEntity, clz);
		} catch (RestClientException e) {
			// TODO Auto-generated catch block
			log.error("错误信息：",e);
		} 
		
		return result;
	}
	
	/**
	 * post方法封装，参数以JSON方式
	 * @param url
	 * @param param
	 * @param clz
	 * @return
	 */
	public <T> T postJsonBody(String url, Object param, Class<T> clz) {
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
		HttpEntity<String> requestEntity = new HttpEntity<String>(JSONObject.toJSONString(param), httpHeaders);
			
		T result = null;
		try {
			result = rest.exchange(url, HttpMethod.POST,requestEntity, clz).getBody();
		} catch (RestClientException e) {
			// TODO Auto-generated catch block
			log.error("错误信息：",e);
		} 
		
		return result;
	}
	

	/**
	 * post方法封装，参数以JSON方式
	 * @param url
	 * @param param
	 * @return
	 */
	public JSONObject postJson(String url, Object param) {
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
		HttpEntity<String> requestEntity = new HttpEntity<String>(JSONObject.toJSONString(param), httpHeaders);
		
		JSONObject result = new JSONObject();
		try {
			ResponseEntity<JSONObject> respon=rest.exchange(url, HttpMethod.POST,requestEntity, JSONObject.class);			
			result = respon.getBody();
		} catch (RestClientException e) {
			//log.error("错误信息：",e);
		} 
		
		return result;
	}

}